﻿using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Forms;
using RaGE_JD.Properties;

namespace RaGE_JD
{
    // Path Object
    internal class PathObject : Object
    {
        // fields
        public int ID;
        public int NameLen;
        public byte[] Name;
    }

    public partial class MainWindow
    {
        private void BtnPathAddClick(object sender, EventArgs e)
        {
            var pObj = new PathObject();

            pObj.ID = int.Parse(txtPathID.Text);

            pObj.NameLen = 0;
            pObj.Name = Encoding.Default.GetBytes(txtPathName.Text);

            path.Add(pObj);

            listPath.Items.Add(new ListViewItem(new[]
            {
                pObj.ID.ToString(CultureInfo.InvariantCulture),
                txtPathName.Text
            }));

            listPath.Items[listPath.Items.Count - 1].EnsureVisible();
        }

        private void BtnPathDeleteClick(object sender, EventArgs e)
        {
            if (listPath.SelectedItems.Count == 0) MessageBox.Show(Resources.SelectPathFirst);
            else
            {
                path.RemoveAt(listPath.SelectedItems[0].Index);
                listPath.Items.RemoveAt(listPath.SelectedItems[0].Index);
            }
        }

        private void BtnPathSaveClick(object sender, EventArgs e)
        {
            if (listPath.SelectedItems.Count == 0) MessageBox.Show(Resources.SelectPathFirst);
            else
            {
                path[listPath.SelectedItems[0].Index].ID = int.Parse(txtPathID.Text);
                path[listPath.SelectedItems[0].Index].NameLen = 0;
                path[listPath.SelectedItems[0].Index].Name = Encoding.GetEncoding("GBK").GetBytes(txtPathName.Text);

                listPath.SelectedItems[0].SubItems[0].Text = txtPathID.Text;
                listPath.SelectedItems[0].SubItems[1].Text = txtPathName.Text;
            }
        }

        private void ListPathClick(object sender, EventArgs e)
        {
            if (listPath.SelectedItems.Count == 0) MessageBox.Show(Resources.SelectFilePathFirst);
            else
            {
                txtPathID.Text = listPath.SelectedItems[0].SubItems[0].Text;
                txtPathName.Text = listPath.SelectedItems[0].SubItems[1].Text;
            }
        }

        private void OpenPath()
        {
            var openPath = new OpenFileDialog();
            openPath.Filter = Resources.PathFilter;
            openPath.Title = Resources.SelectPath;
            openPath.FileName = "path.data";

            if (openPath.ShowDialog() != DialogResult.OK || !File.Exists(openPath.FileName)) return;

            var pathStream = File.OpenRead(openPath.FileName);
            var pathReader = new BinaryReader(pathStream);

            header = pathReader.ReadBytes(4);
            count = pathReader.ReadInt32();

            lblPathMsg.Text = Resources.Total + count.ToString(CultureInfo.InvariantCulture) + Resources.Records;

            for (var i = 0; i < count; i++)
            {
                var pathObject = new PathObject();
                pathObject.ID = pathReader.ReadInt32();
                pathObject.NameLen = pathReader.ReadInt32();
                pathObject.Name = pathReader.ReadBytes(pathObject.NameLen);

                path.Add(pathObject);

                listPath.Items.Add(new ListViewItem(new[]
                        {
                            pathObject.ID.ToString(CultureInfo.InvariantCulture),
                            Encoding.GetEncoding("GBK").GetString(pathObject.Name)
                        }));

                listPath.Items[listPath.Items.Count - 1].EnsureVisible();
            }
        }

        private void SavePath()
        {
            var savePath = new SaveFileDialog();
            savePath.InitialDirectory = Environment.CurrentDirectory;
            savePath.Filter = Resources.PathFilter;
            savePath.FileName = "path.data";

            if (savePath.ShowDialog() != DialogResult.OK) return;

            Cursor = Cursors.WaitCursor;
            progressBar.Style = ProgressBarStyle.Blocks;

            var pStream = File.OpenWrite(savePath.FileName);
            var pWriter = new BinaryWriter(pStream);

            pWriter.Write(header);
            pWriter.Write(path.Count);

            foreach (var t in path)
            {
                pWriter.Write(t.ID);
                pWriter.Write(t.Name.Length);
                pWriter.Write(t.Name);
            }

            pWriter.Flush();
            pWriter.Close();
            pStream.Close();
            savePath.Dispose();

            progressBar.Style = ProgressBarStyle.Continuous;
            Cursor = Cursors.Default;
            MessageBox.Show(Resources.FileSaved);
        }
    }
}
